<?php

/**
 * Created by PhpStorm.
 * User: wx:wenyi2048
 * Date: 2022/4/12 0012
 * Time: 10:28
 */

namespace app\common\service;


use think\Db;
use think\Exception;

class UsersService extends Service
{
    protected $tableName = 'sms_mk_users';

    public function searchPage(&$view, $params, $isdelete = 0)
    {

        $this->db->where('isdelete', '=', $isdelete)
            ->order('id', 'desc')
            ->field('*');
        if (@$params['email']) $this->db->where('email', 'like', '%' . $params['email'] . '%');
        if (@$params['name']) $this->db->where('name', 'like', '%' . $params['name'] . '%');
        if (@$params['phone']) $this->db->where('phone', 'like', '%' . $params['phone'] . '%');

        $this->pageList($view, $params);
    }


    //带注册登录
    public static function sms_login($param)
    {
        $smsCode = $param['code'];
        $phone   = $param['phone'];
        $userInfo = Db::table('sms_mk_sms')
            ->where('code', '=', $smsCode)
            ->where('phone', '=', $phone)
            ->find();
        if (empty($userInfo)) {
            throw new Exception('验证码错误');
        }
        $api_token = 'sms_' . $phone . rand(100000, 999999);
        $api_token = md5($api_token); //api唯一标识
        $user = Db::table('sms_mk_users')
            ->where('phone', '=', $phone)
            ->find();

        //甲方要求可以多台同时登录
        $api_token = $user['api_token'];

        $openId = '';
        /* if(@$param['wx_code']){
            $openId = self::wx_openid($param['wx_code']);
        }*/
        if ($user) {
            //登录
            $data = [
                'api_token' => $api_token,
                'update_time' => time()
            ];
            if ($openId) $data['wx_openid'] = $openId;
            Db::table('sms_mk_users')
                ->where('id', $userInfo['id'])
                //->where('wx_openid',$arr['wx_openid'])
                ->update($data);
            $res = ['user_id' => $userInfo['id'], 'phone' => $phone, 'api_token' => $api_token, 'user' => $user];
            //            return apiSuccess($res,'登录成功');
        } else {
            //注册
            $data = [
                'phone' => $phone,
                'api_token' => $api_token,
                'create_time' => time(),
            ];
            if ($openId) $data['wx_openid'] = $openId;
            $inserId = Db::table('sms_mk_users')->insertGetId($data);
            $res = ['user_id' => $inserId, 'phone' => $phone, 'api_token' => $api_token];
            return apiSuccess($res, '注册成功');
        }
        return  $res;
    }

    public static function wx_openid($wxCode)
    {
        $url = "https://api.weixin.qq.com/sns/jscode2session";
        // 参数
        $params['appid'] = config('weChat')['appid'];
        $params['secret'] = config('weChat')['AppSecret'];
        $params['js_code'] = $wxCode;

        $arr = httpPost($url, $params);
        $arr = json_decode($arr, true);
        //模拟登陆
        //unset($arr['errcode']);$arr['wx_openid'] = 1111111;
        if (isset($arr['errcode']) && !empty($arr['errcode'])) {
            throw new Exception($arr['errcode'] . $arr['errmsg']);
        }

        return  @$arr['openid'];
    }


    /*public function edit()
    {
        //$api_token ='sms_'.$phone.rand(100000, 999999);
        //$api_token = md5($api_token);//api唯一标识

        return parent::edit(); // TODO: Change the autogenerated stub
    }*/
}
